
package org.abbett.eskine.controller;

import org.abbett.eskine.model.CustomIdentity;
import org.abbett.eskine.entity.Page;
import org.abbett.eskine.dao.PageDao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;

import org.jboss.seam.annotations.web.RequestParameter;

import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.event.ActionEvent;
import java.util.Map;
import java.util.List;

import org.abbett.eskine.service.PageDeleteThisPageOutcome;
import org.abbett.eskine.service.PageService;


/**
 * Glue code to bind Wiki Page Service with the view framework (Seam). This class contains
 * a default implementation of the action methods. Please avoid reimplmenting the action methods in this class.
 * The purpose of this class is to declare properties which can be bound to the UI.
 * 
  * Displays a wiki page. If no page ID parameter is provided, displays most recently updated wiki page. If no pages exist, displays new-user welcome message.
  *
 * Automatically generated by clickframes
 * 
 * @author Vineet Manohar
 */
public abstract class GeneratedPageController extends Controller {
 
    @In("#{pageDao}") 
    protected PageDao pageDao;


    /** the Page row which on which the action was performed is stored on 
     * this field via the corresponding action listener if a user performs an action on the 
     * displayed Page list 
     */

    /** Selected UserPages row */
    private Page userPagesSelection;

    /** 
     * Get Selected UserPages
     *  
     * @return Selected UserPages
     */
    public Page getUserPagesSelection() {
       return this.userPagesSelection;
    }

    /**
     * Set Selected UserPages
     *
     * @param userPagesSelection
     */
    public void setUserPagesSelection(Page userPagesSelection) {
        this.userPagesSelection = userPagesSelection;
    }


    public void userPagesListener(ActionEvent actionEvent) {
        UIComponent tmpComponent = actionEvent.getComponent();

        while (null != tmpComponent && !(tmpComponent instanceof UIData)) {
            tmpComponent = tmpComponent.getParent();
        }

        UIData uiData = (UIData) tmpComponent;
        Object selectedRow = uiData.getRowData();
        if (selectedRow instanceof Page) {
            this.userPagesSelection = (Page) selectedRow;
        }
    }


    /** 
                        All issues belonging to the user.
                     */
    private List<Page> userPages;

    /** 
     * Get Pages By User
     *  
     * @return Pages By User
     */
    public List<Page> getUserPages() {
       return this.userPages;
    }

    /**
     * Set Pages By User
     *
     * @param userPages
     */
    public void setUserPages(List<Page> userPages) {
        this.userPages = userPages;
    }


    
    @In("#{pagePageService}")
	protected PageService pageService;

	@SuppressWarnings("unused")
	private static final Log logger = LogFactory.getLog(GeneratedPageController.class);

	@In
	protected CustomIdentity identity;

    @Create
    public void init() {
         // all inputs initialized here 
    }

	@RequestParameter

    /** ${pageParameter.description} */
    private String id;

    /** 
     * Get Page ID
     *  
     * @return Page ID
     */
    public String getId() {
       return this.id;
    }

    /**
     * Set Page ID
     *
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }



    /** Page loaded from param 'id' */
    private org.abbett.eskine.entity.Page page;

    /** 
     * Get Page loaded from param 'id'
     *  
     * @return Page loaded from param 'id'
     */
    public org.abbett.eskine.entity.Page getPage() {
       return this.page;
    }

    /**
     * Set Page loaded from param 'id'
     *
     * @param page
     */
    public void setPage(org.abbett.eskine.entity.Page page) {
        this.page = page;
    }



    protected void initUserPages() {
        // if userPages has not been initialized, initialize it now
        if (getUserPages() == null) {
            setUserPages(loadUserPages());
        }
    }

    /**
     * clears the current value and loads the value again
     */
    protected void reloadUserPages() {
        clearUserPages();
        loadUserPages();
    }

    /**
     * clears the current value of Pages By User 
     */
    protected void clearUserPages() {
        setUserPages(null);
    }

    protected List<org.abbett.eskine.entity.Page> loadUserPages() {
        return pageDao.findUserPages(describe());
    }


	/**
	 * This method should be called before the page is loaded
	 * 
	 * All the page parameters should be consumed in this method
	 */
    public void processPageParameters() {
        initPage();

        initUserPages();


          // if you need to add more code, override this method in the child class 
    }

 
    protected Page loadPage() {
        return pageDao.findByPrimaryKey(id);
    }
    
    protected void initPage() {
        // if page has not been initialized, initialize it now
        if (getPage() == null) {
            setPage(loadPage());
            
         }

    }
 
      	/**
	 * Delete this page
	 *
 	 * Automatically generated by clickframes
	 */
	public abstract PageDeleteThisPageOutcome deleteThisPage();



    private static final long serialVersionUID = 1L;
}// clickframes::version=3567375350::clickframes